package com.gdones.gpt.bus.docs.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.gpt.bus.docs.dto.DocsDto;
import com.gdones.gpt.bus.docs.dto.GptDocs;
import com.gdones.gpt.bus.docs.dto.GptDocsFiles;
import com.gdones.gpt.bus.docs.dto.GptDocsPcs;
import com.gdones.gpt.bus.docs.mapper.GptDocsFilesMapper;
import com.gdones.gpt.bus.docs.mapper.GptDocsMapper;
import com.gdones.gpt.bus.docs.mapper.GptDocsPcsMapper;
import com.gdones.gpt.bus.docs.service.IGptDocsFilesService;
import com.gdones.gpt.bus.docs.service.IGptDocsPcsService;
import com.gdones.gpt.bus.docs.service.IGptDocsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.gpt.bus.mask.dto.GptMask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-06-10
 */
@Service
@Transactional
public class GptDocsServiceImpl extends ServiceImpl<GptDocsMapper, GptDocs> implements IGptDocsService {

    @Autowired
    private GptDocsMapper docsMapper;

    @Autowired
    private GptDocsFilesMapper filesMapper;

    @Autowired
    private GptDocsPcsMapper pcsMapper;

    @Resource(name = "gptDocsFilesServiceImpl")
    private IGptDocsFilesService gptDocsFilesServiceImpl;

    @Resource(name = "gptDocsPcsServiceImpl")
    private IGptDocsPcsService gptDocsPcsServiceImpl;

    @Override
    public MyResult mypage(GptDocs dto, MyPageBand pageBand) {

        MyResult result = new MyResult();

        IPage<GptDocs> pages = docsMapper.pages(PageDTO.of(pageBand.getPage(), pageBand.getSize()), dto);

        // 根据分页的结果封装返回数据
        MyPager<GptDocs> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setData(pager);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult mylist(GptDocs dto) {

        MyResult result = new MyResult();

        List<GptDocs> list = docsMapper.myList(dto);

        result.setData(list);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult getByID(String id) {

        MyResult result = new MyResult();

        DocsDto dto = new DocsDto();
        // 1. 文档信息
        GptDocs byId = this.getById(id);
        // 2. 附件信息
        QueryWrapper<GptDocsFiles> filesQueryWrapper = new QueryWrapper<>();
        filesQueryWrapper.eq("docID",id);
        List<GptDocsFiles> gptDocsFiles = filesMapper.selectList(filesQueryWrapper);
        // 3.图片信息
        QueryWrapper<GptDocsPcs> pcsQueryWrapper = new QueryWrapper<>();
        pcsQueryWrapper.eq("docID",id);
        List<GptDocsPcs> gptDocsPcs = pcsMapper.selectList(pcsQueryWrapper);

        dto.setDocs(byId);
        dto.setPcs(gptDocsPcs);
        dto.setFiles(gptDocsFiles);

        result.setData(dto);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult add(DocsDto dto) {

        MyResult result = new MyResult();
        // 1. 文档信息保存
        GptDocs docs = dto.getDocs();
        docs.setDocID(IdUtil.fastSimpleUUID());

        // 2. 附件保存
        List<GptDocsFiles> files = dto.getFiles();
        for(GptDocsFiles file:files){
            file.setDocID(docs.getDocID());
        }

        // 3. 图片保存
        List<GptDocsPcs> pcs = dto.getPcs();
        for(GptDocsPcs pc :pcs){
            pc.setDocID(docs.getDocID());
        }

        boolean save = this.save(docs);
        if(!save){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setCode("9999");
            result.setMsg("添加出错：文章保存出错！");
            return result;
        }

        boolean b1 = gptDocsPcsServiceImpl.saveBatch(pcs);
        if(!b1){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setCode("9999");
            result.setMsg("添加出错：文章图片保存出错,请至少添加一个配图！");
            return result;
        }

        boolean b = gptDocsFilesServiceImpl.saveBatch(files);

        result.setMsg("添加成功！");

        return result;
    }

    @Override
    public MyResult update(DocsDto dto) {

        MyResult result = new MyResult();

        // 1. 文档信息保存
        GptDocs docs = dto.getDocs();

        // 删除所有的文件附件从新保存记录
        int i = filesMapper.realDeleteByDocsID(docs.getDocID());
        int i2 = pcsMapper.realDeleteByDocsID(docs.getDocID());


        // 2. 附件保存
        List<GptDocsFiles> files = dto.getFiles();
        for(GptDocsFiles file:files){
            file.setDocID(docs.getDocID());
        }

        // 3. 图片保存
        List<GptDocsPcs> pcs = dto.getPcs();
        for(GptDocsPcs pc :pcs){
            pc.setDocID(docs.getDocID());
        }

        boolean save = this.updateById(docs);
        if(!save){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setCode("9999");
            result.setMsg("修改出错：文章保存出错！");
            return result;
        }

        boolean b = gptDocsFilesServiceImpl.saveBatch(files);

        boolean b1 = gptDocsPcsServiceImpl.saveBatch(pcs);
        if(!b1){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result.setCode("9999");
            result.setMsg("修改出错：文章图片保存出错！请至少选择一张图片");
            return result;
        }

        result.setMsg("修改成功！");

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);

        boolean b = this.removeBatchByIds(list);
        if(b){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }
}
